package org.lanqiao.api.user;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.lanqiao.common.Constants;
import org.lanqiao.common.JsonResult;
import org.lanqiao.common.JsonResultWriter;
import org.lanqiao.service.UserServiceImpl;
import org.lanqiao.util.HttpUtil;
import org.lanqiao.util.RandomUtil;
import org.lanqiao.util.SqlUtil;
import org.lanqiao.util.StringUtil;

/**
 * Servlet implementation class LoginTelphoneServlet
 */
@WebServlet(description = "手机号登陆的接口", urlPatterns = { "/api/user/telLogin" })
public class LoginTelphoneServlet extends HttpServlet {

    private static String operation = "/industrySMS/sendSMS";
    
    private static Properties properties =new Properties();
    //加载配置文件,load  类加载器
    static {
        try {
            properties.load(SqlUtil.class.getClassLoader().getResourceAsStream("config.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static String accountSid = properties.getProperty("ACCOUNT_SID");   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginTelphoneServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	    String telphone = StringUtil.empty(request.getParameter("telphone"));
        //创建一个JsonResult封装消息，然后转为json，变成一个对象{}
        JsonResult result=null;
        //获得当前请求的Session对象，如果上下文中没有绑定的Session，创建一个session，如果有就返回session
        HttpSession session=request.getSession();
        UserServiceImpl usi = new UserServiceImpl();
        List list = null;
        if(telphone==null) {
            result=new JsonResult<>("手机号不能为空", Constants.STATUS_UNFOUND);
        }else {
            try {
                System.out.println("电话是："+telphone);
                list = usi.loginTel(telphone);
                if(list!=null&&list.size()>0) {
                    String code=RandomUtil.randomNum(6);
                    String tmpSmsContent = null;
                    
                    String smsContent = "【昭坪科技】您的验证码为"+code+"，请于30分钟内正确输入，如非本人操作，请忽略此短信。";
                     try{
                       tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8");
                     }catch(Exception e){
                       e.printStackTrace();
                     }
                     
                     String url = properties.getProperty("BASE_URL") + operation;
                     String body = "accountSid=" + accountSid + "&to=" + telphone + "&smsContent=" + tmpSmsContent
                         + HttpUtil.createCommonParam();

                     // 提交请求
                     String backresult = HttpUtil.post(url, body);
                     
                     System.out.println("result:" + System.lineSeparator() + backresult);
                     HashMap map = new HashMap<>();
                     map.put("code", code);
                     map.put("backresult", backresult);
                     list.add(map);
                    
                    result=new JsonResult<>("手机号已注册过,可以登陆", Constants.STATUS_SUCCESS,list);
                }else {
                    result=new JsonResult<>("手机号未注册,请先注册再登陆", Constants.STATUS_UNFOUND);
                }
            }catch(Exception e) {
                e.printStackTrace();
                result=new JsonResult<>("手机号查询异常", Constants.STATUS_FAILURE);
            } 
        }
        //将响应和结果  数据，转为json
        JsonResultWriter.writer(response, result);
	}

}
